package com.jason.seckill.controller;

import com.jason.seckill.pojo.User;
import com.jason.seckill.service.IGoodsService;
import com.jason.seckill.service.IUserService;
import com.jason.seckill.utils.RedisUtil;
import com.jason.seckill.vo.DetailVo;
import com.jason.seckill.vo.GoodsVo;
import com.jason.seckill.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ThymeleafViewResolver viewResolver;

    //window优化前qps:842.3
    //window第一次优化qps:1461.1
    @RequestMapping(value = "/toList", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model, User user, HttpServletRequest request, HttpServletResponse response) {
        //其实这些页面静态化应该是前后端分离时交给前端处理的？
        String html = (String) redisUtil.get("goodsList");
        //不为空就返回当前页面
        if (!StringUtils.isEmpty(html)) {
            return html;
        }
        model.addAttribute("user", user);
        model.addAttribute("goodsList", goodsService.findGoodsVo());
        //空则手动就手动渲染
        WebContext webContext = new WebContext(request, response,
                request.getServletContext(), request.getLocale(),
                model.asMap());
        html = viewResolver.getTemplateEngine().process("goodsList", webContext);
        if (!StringUtils.isEmpty(html)) {
            redisUtil.set("goodsList", html, 60);
        }
        return html;
    }

    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(User user, @PathVariable Long goodsId) {
        GoodsVo goods = goodsService.findGoodsVoByGoodsId(goodsId);
        Date startDate = goods.getStartDate();
        Date endDate = goods.getStopDate();
        Date nowDate = new Date();
        //秒杀状态
        int secKillStatus = 0;
        //剩余开始时间
        int remainSeconds = 0;
        //秒杀还未开始
        if (nowDate.before(startDate)) {
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
            // 秒杀已结束
        } else if (nowDate.after(endDate)) {
            secKillStatus = 2;
            remainSeconds = -1;
            // 秒杀中
        } else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
        DetailVo detailVo = new DetailVo();
        detailVo.setGoodsVo(goods);
        detailVo.setUser(user);
        detailVo.setRemainSeconds(remainSeconds);
        detailVo.setSecKillStatus(secKillStatus);
        return RespBean.success(detailVo);
    }

    //@RequestMapping(value = "/toDetail2/{goodsId}", produces = "text/html;charset=utf-8")
    //@ResponseBody
    //public String toDetail2(Model model, User user, @PathVariable long goodsId,
    //                        HttpServletRequest request, HttpServletResponse response) {
    //    //其实我觉得这个页面做缓存意义不大啊，因为倒计时变化的话,页面还是会不断刷新，根本不会走redis
    //    String html = (String) redisUtil.get("goodsDetail:" + goodsId);
    //    if (!StringUtils.isEmpty(html)) {
    //        return html;
    //    }
    //    if (user == null) {
    //        return "login";
    //    }
    //    //秒杀状态
    //    int secKillStatus = 0;
    //    int remainSeconds = 0;
    //    GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
    //    Date startDate = goodsVo.getStartDate();
    //    Date stopDate = goodsVo.getStopDate();
    //    Date nowDate = new Date();
    //    //秒杀还未开始
    //    if (nowDate.before(startDate)) {
    //        remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
    //        secKillStatus = 0;
    //    } else if (nowDate.after(stopDate)) {
    //        //秒杀结束
    //        secKillStatus = 2;
    //    } else {
    //        //秒杀中
    //        secKillStatus = 1;
    //        remainSeconds = 0;
    //    }
    //    //这里的user就是我们之前配置那些config的时候会带上的
    //    System.out.println(secKillStatus);
    //    model.addAttribute("user", user);
    //    model.addAttribute("remainSeconds", remainSeconds);
    //    model.addAttribute("secKillStatus", secKillStatus);
    //    model.addAttribute("goods", goodsVo);
    //    WebContext webContext = new WebContext(request, response,
    //            request.getServletContext(), request.getLocale(),
    //            model.asMap());
    //    html = viewResolver.getTemplateEngine().process("goodsDetail", webContext);
    //    if (!StringUtils.isEmpty(html)) {
    //        redisUtil.set("goodsDetail" + goodsId, html, 60);
    //    }
    //    return html;
    //}
}
